<%--
  Created by IntelliJ IDEA.
  User: teacher
  Date: 2022/4/24
  Time: 14:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文件上传</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.4.1.min.js"></script>
    <script>
        $(function(){
            $("#btn").click(function(){
                // 获取表单的数据
                // 使用JS提供的一个对象FormData
                // 构造函数的参数是对应的表单
                // 参数必须是DOM对象
                let formData = new FormData($("#addForm")[0]);
                $.ajax({
                    type:"post",
                    url:"${pageContext.request.contextPath}/upload",
                    data:formData,
                    dataType:"text",
                    processData:false,
                    contentType:false,
                    success:function(result){
                        console.log(result);
                    }
                })
            })
        })
    </script>
</head>
<body>
<form id="addForm">
    用户名:<input type="text" name="username"/><br/>
    文件:<input type="file" name="file"/><br/>
    <input type="button" id="btn" value="上传">
</form>
</body>
</html>
